package com.eascs.spider.hr.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.bson.Document;

import com.eascs.spider.hr.user.entity.ResultProj;
import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;

public class MongodbUtil {
	
	private static MongoClient mongoClient = null;
	private static MongoDatabase mongoDatabase = null;
	
	//公司配置
  static{
	    Map<String,String> mgo = DBUtil.getProperties();
        ServerAddress serverAddress = new ServerAddress(
        		mgo.get("mongodb.ip"),Integer.valueOf(mgo.get("mongodb.port"))); 
        List<ServerAddress> addrs = new ArrayList<ServerAddress>();  
        addrs.add(serverAddress);  
        MongoCredential credential = MongoCredential.createScramSha1Credential(
        		mgo.get("mongodb.user"), mgo.get("mongodb.library"), 
        		mgo.get("mongodb.password").toCharArray());  
        
        List<MongoCredential> credentials = new ArrayList<MongoCredential>();  
        credentials.add(credential);  
        mongoClient = new MongoClient(addrs,credentials);  
        mongoDatabase = mongoClient.getDatabase(mgo.get("mongodb.library"));  
	}
	
	//家里配置
	/*static{
        mongoClient = new MongoClient("192.168.14.131" , 27017 );  
        mongoDatabase = mongoClient.getDatabase("spider");  
	}*/
	
	private static void set(String collectionName ,Map<String, Object> map){
		MongoCollection<Document> mongoCollection = mongoDatabase.getCollection(collectionName);
		Document document = new Document(map);
		mongoCollection.insertOne(document);
	}
	
	/**
	 * 检索指定集合中所有文档
	 * @param collectionName
	 */
	/*private static void get(String collectionName){
		MongoCollection<Document> mc = mongoDatabase.getCollection(collectionName);
		FindIterable<Document> findIterable = mc.find();  
        MongoCursor<Document> mongoCursor = findIterable.iterator();
        Map<String, String> map = new HashMap<>();
        int count =0;
        while(mongoCursor.hasNext()){  
           Document document = mongoCursor.next();
           if(!map.containsKey(document.getString("ID"))){
        	   map.put(document.getString("ID"), "");
           }else{
        	   count++;
        	   System.out.println(document.getString("ID"));
           }
           
        }  
	}*/
	
	public static void set51job(Map<String, Object> data){
		set("51job", data);
	}
	
	public static void setZhilian(Map<String, Object> data){
		set("zhilian", data);
	}
	
	
	//51job条件查询,翻页查询
	public static ResultProj query51job(String expAdress , String keyword 
			, String function ,String update_time ,int ageMin ,int ageMax 
			,int currentPage , int pageSize){
		MongoCollection<Document> collection = mongoDatabase.getCollection("51job");
		BasicDBObject queryObject = new BasicDBObject();
		
		
		if(!StringUtil.isNullOrEmpty(expAdress)){
			Pattern patExpAdd = Pattern.compile(expAdress,Pattern.CASE_INSENSITIVE);
			queryObject.append("期望工作地区", patExpAdd);
		}
		if(!StringUtil.isNullOrEmpty(keyword)){
			Pattern patKeyWord = Pattern.compile(keyword.substring(0, 1),Pattern.CASE_INSENSITIVE);
			queryObject.append("关键词", patKeyWord);
		}

		if(!StringUtil.isNullOrEmpty(function)){
			Pattern patFunc = Pattern.compile(function);
			queryObject.append("职能", patFunc);
		}
		
		if(!StringUtil.isNullOrEmpty(update_time)){
			Pattern patUpdate = Pattern.compile(update_time);
			queryObject.append("简历更新时间", patUpdate);
		}
		
		FindIterable<Document> findIterable = collection.find(queryObject).skip((currentPage -1)*pageSize).limit(pageSize);
		MongoCursor<Document> mongoCursor = findIterable.iterator();  
		
		List<Document> list = new ArrayList<>();
		if(ageMax==0 && ageMin==0){
			while(mongoCursor.hasNext()){  
	            Document document = mongoCursor.next();
	        	list.add(document);  
	        }
		}else{
			while(mongoCursor.hasNext()){  
	            Document document = mongoCursor.next();
	            Integer age = Integer.valueOf(document.getString("年龄"));
	            if(age.intValue()<ageMin || age.intValue()> ageMax)
	            	continue;
	        	list.add(document);  
	        }
		}
		
		int totalPage = (int)collection.count(queryObject);
		ResultProj resultProj = new ResultProj();
		resultProj.setTotalPage(totalPage/15 +1);
		resultProj.setCurrentPage(currentPage);
		resultProj.setDocuments(list);
        return resultProj;
	}
	
	
	//根据id查51简历
	/*public static List<Document> query51job(String id){
		return query51job(id, null, null, null, 0, 0,1,10);
	}*/
	
	//根据除id之外的其他条件查51简历
	/*public static List<Document> query51job(String expAdress , String keyword , String function ,int ageMin ,int ageMax ){
		return query51job(null, expAdress, keyword, function, ageMin, ageMax,1,50);
	}*/
	
	public static void update51job(String id ,String address){
		MongoCollection<Document> collection = mongoDatabase.getCollection("51job");
		collection.updateMany(Filters.eq("ID", id), new Document("$set",new Document("期望工作地区" ,address)));
	}
	public static void main(String[] args) {
		query51job(null, "支行行长", null ,"201709011",30, 50 ,1 ,20);
	}
}
